package com.lightsh.bitmap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.util.Base64;
/**
 * 该类包含图片的处理方式
 * @author Light
 *
 */
public class ImageUtility {
	/**
	 * 给图片添加倒影
	 * 原理:先翻转图片，由上到下放大透明度
	 */
	public static Bitmap createReflectedImage(Bitmap srcImage) {
		if(srcImage == null){
			return null;
		}
		int width = srcImage.getWidth();
		int height = srcImage.getHeight();
		
		// 原图片的底部与倒影间的垂直距离
		final int reflectionGap = 4;
		final int reflectHeight = height / 8;

		Matrix matrix = new Matrix();
		
		// 先进行翻转 操作
		matrix.preScale(1, -1);

		// 创建倒影的图片
		Bitmap reflectionImage = Bitmap.createBitmap(srcImage, 0, height - reflectHeight, width, reflectHeight, matrix, false);

		// Create a new bitmap with same width but taller to fit reflection
		// 创建一张图片，是目标图片的大小
		Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + reflectHeight + reflectionGap), Config.ARGB_8888);

		// 使用目标图片初始化canvas
		Canvas canvas = new Canvas(bitmapWithReflection);
		// 画原始图片
		canvas.drawBitmap(srcImage, 0, 0, null);
		// 画间隔
		Paint defaultPaint = new Paint();
		canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
		// 画倒影
		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

		// 创建线性的变化
		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, 
				height, 0, bitmapWithReflection.getHeight(),
				0x70ffffff, 0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		
		// 设置画的模式(与操作?)
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		// 话
		canvas.drawRect(0, height + reflectionGap, width, bitmapWithReflection.getHeight(), paint);
		return bitmapWithReflection;
	}
	
	//drawable 类型转化为bitmap
	public static Bitmap drawableToBitmap(Drawable drawable) {
		if(drawable == null){
			return null;
		}
		Bitmap bitmap = Bitmap.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}
	
	public static Bitmap scallBitmap(Bitmap srcBitmap, float scallRange){
		if(srcBitmap == null){
			return null;
		}
		int width = srcBitmap.getWidth();
		int height = srcBitmap.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale(scallRange, scallRange);
		return Bitmap.createBitmap(srcBitmap, 0, 0, width, height, matrix, true);
	}
	
	public static byte[] bitmapToPNGBytes(Bitmap bitmap){
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
			return out.toByteArray();
		}
		return null;
	}
	
	// 将资源图片转成可网络传输的byte[],即做base64编码
    public static byte[] resourceToBytes(Resources res, int bitmapId){
    	Bitmap bmp = BitmapFactory.decodeResource(res, bitmapId);
    	return Base64.encode(bitmapToPNGBytes(bmp), Base64.DEFAULT);
    	
    }
    
    // 将图片文件转换成byte[],不成功则返回null
    public static byte[] bitmapFileToBytes(String filePath){
    	File file = new File(filePath);
    	if(!file.exists() || !file.isFile()){
    		return null;
    	}
    	try{
    		FileInputStream in = new FileInputStream(file);
        	ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        	byte[] buffer = new byte[1024];
        	while (true) {
    			int nRead = in.read(buffer);
    			if(nRead == -1){
    				break;
    			}
    			bytesOut.write(buffer, 0, nRead);
    		}
        	return Base64.encode(bytesOut.toByteArray(), Base64.DEFAULT);
    	}catch(IOException e){
    		e.printStackTrace();
    		return null;
    	}
    }
    
    public static final Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundX, float roundY) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundX, roundY, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;     
    }
}
